#ifndef Threadpoll_h
#define Threadpoll_h
#include "Thread.hpp"
#include "Cond.hpp"
#include <iostream>
#include <memory>
#include <vector>
#include <queue>
using func_t = std::unique_ptr<Thread>;
using std::cout;
using std::endl;

int gnum = 4;
void zgnb()
{
    cout << "钟哥nb" << endl;
}
template <class T>
class Threadpool
{
private:
    Threadpool(int num = gnum)
        : _pnum(num), _waitnum(0), _isrunning(false)
    {
        for (int i = 0; i < num; i++)
        {
            _vpthread.push_back(std::make_unique<Thread>());
        }
    }

public:
    Threadpool(const Threadpool &data) = delete;
    Threadpool &operator=(const Threadpool &data) = delete;
    bool Isempty() { return _qfunc.empty(); }
    void Task()
    {
        while (true)
        {
            // 获取任务才是访问临界资源
            T t;
            {
                zgw::LockGuard mu(_mutex);
                while (Isempty() && _isrunning) // 只有不会
                {
                    _waitnum++;
                    cout<<"正在等待"<<endl;
                    _cond.Wait(_mutex);
                    cout<<"退出等待"<<endl;
                    cout<<"isrunning"<<_isrunning<<endl;
                    _waitnum--;
                }
                if (Isempty() && !_isrunning)
                {
                    cout<<"线程退出"<<endl;
                    break;
                }
                t = _qfunc.front();
                _qfunc.pop();
                cout << "队列任务还剩" << _qfunc.size() << endl;
            }
            t();
        }
    }

    void Equeue(T t)
    {
        if (!_isrunning)
            return;
        _qfunc.push(t);
        if (_waitnum > 0)
            _cond.Broadcast();
    }

    void Init()
    {
        for (auto &ptr : _vpthread)
        {
            ptr->setFunc(std::bind(&Threadpool<T>::Task, this));
            cout << "初始线程函数";
            cout << ptr->name() << endl;
        }
    }

    void Start()
    {
        _isrunning = true;
        cout << "线程执行任务" << endl;
        for (auto &ptr : _vpthread)
        {
            ptr->start();
        }
    }

    void Stop()
    {
        if (_isrunning)
        {
            _isrunning = false;
            if (_waitnum) // 如果有在wait的线程就唤醒他们,他们执行完就退出
            {
                cout<<"呼唤退出"<<endl;
                _cond.Broadcast();
            }
        }
    }

    void Wait()
    {
        for (auto &ptr : _vpthread)
        {
            cout << "等待退出" << endl;
            ptr->wait();
        }
    }

    ~Threadpool()
    {
    }

    static Threadpool<T> *Getptr()
    {
        if (_threadptr == nullptr)
        {
            if (_threadptr == nullptr)
            {
                _threadptr = new Threadpool<T>();
            }
        }
        return _threadptr;
    }

private:
    int _waitnum;
    int _pnum;

    std::vector<func_t> _vpthread;
    std::queue<T> _qfunc;
    bool _isrunning;
    zgw::Cond _cond;
    zgw::Mymutex _mutex;
    static Threadpool<T> *_threadptr;
};

template <class T>
Threadpool<T> *Threadpool<T>::_threadptr = nullptr;
#endif